var city = require('/assets/js/city.js');
Page({
  data: {
    id: null,
    step: 1,
    focus: "",
    cityShow: false,
    cityData: [],
    range: [],
    citys: [],

    info: {
      "CustomerId": 0,
      "CustomerName": "",
      "Mb": "",
      "EmployeeId": "",
      "EmployeeName": "",
      "CustomerTypeId": "",
      "CustomerQuTypeId": "",
      "ComapnyAddress": "",
      "Phone": "",
      "Fax": "",
      "Email": "",
      "Remark": "",
      "Weichat": "",
      "Intention": "",
      "FromSource": "",
      "ProvinceId": "",
      "ProvinceName": "",
      "CityId": "",
      "CityName": "",
      "CountyId": "",
      "CountyName": "",
      "From": {},
      "IntentionName": {}
    }

  },

  //   info: {
  //   "CustomerId": 0,
  //   "CustomerName": "",
  //   "Mb": "",
  //   "EmployeeId": null,
  //   "CustomerTypeId": null, 
  //   "ComapnyAddress": "",
  //   "Phone": "",
  //   "Fax": "",  
  //   "Email": "",
  //   "Remark": "",  
  //   "Weichat": "",
  //   "Intention": null,
  //   "FromSource": null,
  //   "ProvinceId": null,
  //   "ProvinceName": "",
  //   "CityId": null,
  //   "CityName": "",
  //   "CountyId": null,
  //   "CountyName": "" ,

  //   }
  // },
  onLoad(e) {
    console.log(e);
    if (e.id) {
      this.setData({
        id: e.id
      })
      this.getInfo()
    }
    this.setData({
      cityData: city.cityData,
      // City: city.cityData[this.data.range[0]].children,
      // County: city.cityData[this.data.range[0]].children[this.data.range[1]].children
    })

  },

  toSlt(e) {
    console.log(e);

    
    var str = "?"
    for (const i in e.target.dataset) {
      str+= i+"="+e.target.dataset[i]+"&"
    }
    str = str.substr(0,str.length-1)
    if(str.length <= 1){
      return;
    }
    // var str = "?"
    // for (const i in e.target.dataset) {
    //   str += i + "=" + e.target.dataset[i]
    // }

    // console.log(str)
    // if (str.length == 1) {
    //   return;
    // }
    dd.navigateTo({
      url: "/pages/marks/sltradio/sltradio" + str
    })
  },
  getInfo() {
    var _this = this
    getApp().req({
      url: "/customer/" + this.data.id,
      cb(res) {
        console.log(res)
        _this.setData({ info: res.data })
        console.log(_this.data.info)
      }
    })

  },

  Fromtoemp(e) {
    var str = "?"
    for (const i in e.target.dataset) {
      str += i + "=" + e.target.dataset[i]
      var arr = str.split('');
      arr.splice(16, 1, '&');
      str = arr.join('')
    }

    if (str.length == 1) {
      return;
    }
    dd.navigateTo({
      url: "/pages/marks/sltradio/sltradio" + str
    })
  },

  Intent(e) {
    var str = "?"
    for (const i in e.target.dataset) {
      str += i + "=" + e.target.dataset[i]
      var arr = str.split('');
      arr.splice(15, 1, '&');
      str = arr.join('')
    }

    if (str.length == 1) {
      return;
    }
    dd.navigateTo({
      url: "/pages/marks/sltradio/sltradio" + str
    })
  },

  toemp(e) {
    dd.navigateTo({
      url: "/pages/marks/empslt/empslt?role=" + e.target.dataset.name
    })
  },

  toMark(e) {
    dd.navigateTo({
      url: "/pages/marks/empslt/empslt"
    })
  },
  citySlt() {
    this.setData({
      cityShow: true
    })
  },
  onGetCity(e) {
    var cityarr = [e[0][0], e[1][0], e[2][0]]
    var newinfo = this.data.info
    newinfo.ProvinceId = e[0][0]
    newinfo.ProvinceName = e[0][1]
    newinfo.CityId = e[1][0]
    newinfo.CityName = e[1][1]
    newinfo.CountyId = e[2][0]
    newinfo.CountyName = e[2][1]
    this.setData({
      cityShow: false,
      citys: cityarr,
      info: newinfo
    })

  },
  changeValue(e) {
    var aftdata = this.data.info
    if (e.target.dataset.parent) {
      aftdata[e.target.dataset.parent][e.target.dataset.key] = e.detail.value
    } else {
      aftdata[e.target.dataset.key] = e.detail.value
    }
    this.setData({ info: aftdata })
  },

  openboard(e) {
    var _this = this
    setTimeout(() => {
      _this.setData({ focus: e.target.dataset.key })
    }, 100);
  },

  closeboard() {
    dd.hideKeyboard();
  },

  btn() {
       

    if (!this.data.info.CustomerName) {
      dd.showToast({
        type: 'fail',
        content: '渠道商名称不能为空'
      });
      return;
    }

    if (this.data.info.CustomerQuTypeId !== 22 && this.data.info.CustomerTypeId !== 22 && this.data.info.CustomerTypeId !== 23 && this.data.info.CustomerQuTypeId !== 23) {
      dd.showToast({
        type: 'fail',
        content: '渠道商类型不能为空'
      });
      return;
    }

    if (this.data.info.FromSource == '') {
      dd.showToast({
        type: 'fail',
        content: '渠道商来源不能为空'
      });
      return;
    }
   if (this.data.info.Intention == '') {
      dd.showToast({
        type: 'fail',
        content: '渠道商意向不能为空'
      });
      return;
    }
 if (this.data.info.Mb == '') {
      dd.showToast({
        type: 'fail',
        content: '联系电话不能为空'
      });
      return;
    }

     if (!(/^1[345789]\d{9}$/.test(this.data.info.Mb))) {
        dd.showToast({
        type: 'fail',
        content: '联系电话有误'
      });
      return;
    }

 if (this.data.info.EmployeeId == '') {
      dd.showToast({
        type: 'fail',
        content: '业务员不能为空'
      });
      return;
    }

    // this.data.info.CustomerTypeId = this.data.info.CustomerQuTypeId
    // this.data.info.CustomerTypeName = this.data.info.CustomerQuTypeName
    // this.data.info.Intention = this.data.info.Intention + ''
    // delete this.data.info.IntentionName
    
    //  delete this.data.info.CustomerQuTypeName
    //  delete this.data.info.CustomerQuTypeId
    var _this = this
    getApp().req({
      url: "/customer/channelcustomeredit",
      type: "POST",
      data: this.data.info,
      cb(res) {
        if (res.status == 200 ) {
          // dd.showToast({
          //   type: 'success',
          //   content: '修改成功',
          //   success() {
          //   }
          // })

          dd.redirectTo({
              url: '../crmchannel/crmchannel'
          })
            

          } 


      }
    })
  }

});


      // if (_this.data.info.CustomerId == 0) {
      //   dd.redirectTo({
      //     url: '../crmchannel/crmchannel'
      //   })
      // } else {
      //   dd.navigateBack()
      // }
//  customerSave() {
//     console.log(this.data.info);
//     var _this = this
//     getApp().req({
//       url: "/customer/customeredit",
//       type: "POST",
//       data: this.data.info,
//       cb(res) {
//         console.log(res);
        
//         if(_this.data.info.CustomerDemandId == 0){
//           dd.redirectTo({
//             url: '../crmcustomer/crmcustomer'
//           })
//         }else{
//           dd.navigateBack()
//         }
//       }
//     })
    
//   }